<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <!-- v-auth="'wms:rd:order:rdOrder:edit'" -->
      <template #toolbar>
        <a-button type="default" @click="handlePrint()">
          <Icon icon="ant-design:printer-twotone" /> {{ t('打印') }}
        </a-button>
      </template>

      <!-- <template #firstColumn="{ record }">
        <a @click="handleForm({ djno: record.parent.djno })">
          {{ record.parent.djno }}
        </a>
      </template> -->
      <template #combinedProducts="{ record }">
        <div v-html="handlecombinedProducts(record.invName)"> </div>
      </template>
      <template #phoneFormat="{ record }">
        <div v-html="handlePhoneFormat(record.phone)"> </div>
      </template>
      <!--  -->
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts" setup name="ViewsWmsRdOrderRdOrderList">
  import { unref, onMounted, computed } from 'vue';
  import { Table } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { rdOrderDelete, rdOrderSubListData, hisListData } from '/@/api/wms/rd/order/rdOrder';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { companyTreeData } from '/@/api/sys/company';
  import InputForm from './form.vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { useUserStore } from '/@/store/modules/user';
  import PrintModal from '/@/components/print/printModal.vue';
  import { useModal } from '/@/components/Modal';
  const userStore = useUserStore();

  const { t } = useI18n('wms.rd.order.rdOrder');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('出库订单明细管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('出库单号'),
        field: 'orderNo',
        component: 'Input',
      },
      {
        label: t('收货人'),
        field: 'shr',
        component: 'Input',
      },
      {
        label: t('电话'),
        field: 'phone',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('出库单号'),
      dataIndex: 'orderNo',
      key: 'a.orderNo',
      // sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('地址'),
      dataIndex: 'address',
      key: 'a.address',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('收货人'),
      dataIndex: 'shr',
      key: 'shr',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('电话'),
      dataIndex: 'phone',
      key: 'a.phone',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'phoneFormat',
    },
    {
      title: t('商品名称'),
      dataIndex: 'invName',
      key: 'a.invName',
      // sorter: true,
      width: 130,
      align: 'left',
      slot: 'combinedProducts',
    },
    {
      title: t('数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      // sorter: true,
      width: 130,
      align: 'left',
      showSummary: true,
    },
  ];

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getDataSource, setProps, getSelectRows }] = useTable({
    api: hisListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    rowKey: 'id',
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    showCustomSummary: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/wms/rd/order/rdOrder/exportSubData',
      params: getForm().getFieldsValue(),
    });
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  onMounted(async () => {
    setProps();
    handleCompany();
    getHomePage();
  });

  async function handleCompany() {
    await getForm().setFieldsValue({
      'parent.companyCode': userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      'parent.companyCode': userStore.getProjecte.code,
    });
  }

  async function getHomePage() {
    if (router.currentRoute.value.query.homePage == 'true') {
      await getForm().setFieldsValue({
        status: '0',
      });
      reload();
    }
  }

  const totalFqty = computed(() => {
    const tableData = getDataSource();
    // const tableData = getDataSource.value;
    let sum = tableData.reduce((total, item) => total + (item.fqty || 0), 0);
    return sum;
  });

  const totalFweight = computed(() => {
    const tableData = getDataSource();
    // const tableData = getDataSource.value;
    let sum = tableData.reduce((total, item) => total + (item.fweight || 0), 0);
    sum = sum.toFixed(2);
    return sum;
  });

  function handlecombinedProducts(value) {
     if (value === undefined || value === null) {
      return '';
    }
    let newStr = value.replace(/\n/g, '<br>'); // 替换所有换行符为 <br>
    return newStr;
  }

  function handlePhoneFormat(value) {
     if (value === undefined || value === null) {
      return '';
    }
    let newStr = value.replace(/、/g, '<br>'); // 替换所有换行符为 <br>
    return newStr;
  }

  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  async function handlePrint() {
    // console.log('提交数据', props.rdOrderPrintList[selectedIndex.value]);
    if (getSelectRows().length == 0) {
      showMessage(t('请先选择数据'));
      return;
    }
    // 每个id 前面加上 ids:' + item.id + ','
    const ids = getSelectRows().map((item) => item.id).join(',');
    console.log('ids', ids);
    let params = {
      ids: 'ids:' + ids,
      title: '封箱标签打印',
      height: '0.9',
      width: '0.7',
      fileName: '标签打印fx_100',
      uuid: '',
    };
    openPrintModal(true, params);
  }
</script>
